require 'sketchup.rb'
module AMTF_mixin
  def 返回第一个选择对象
    设置常用对象
    if @selection.count == 0
      UI.messagebox "请先选择一个 拟导出对象 ！"
    else
      return 对象=@selection[0]
    end
  end

  def 返回全部或选择对象
    model = Sketchup.active_model
    selection = Sketchup.active_model.selection
    if selection.count == 0
      entities = model.active_entities
    else
      entities=selection
    end
    return entities
  end

  def 返回选择对象
    设置常用对象
    if @selection.count == 0
      UI.messagebox "请先选择一个 拟导出对象 ！"
    else
      return @selection
    end
  end

  def 设置常用对象
    @model = Sketchup.active_model
    @entities = @model.active_entities
    @selection = @model.selection

    if @entities[0].parent.kind_of? Sketchup::Model
      @编辑中组件名称=nil
    else
      @编辑中组件=@entities[0].parent
      @编辑中组件名称=@编辑中组件.name
    end

    @编辑中tr=@model.edit_transform
    @原点 = Geom::Point3d.new(0,0,0)
    @编辑中局部原点=@原点.transform(@编辑中tr)
    @definitions = @model.definitions

  end
  def get_dir
    p "hello AF!"
    file = __FILE__.dup
    file.force_encoding("UTF-8")
    if file.respond_to?(:force_encoding)
      f = File.basename(file, '.*').freeze
      p "File.basename="+f
      r = File.dirname(file).freeze+"/组件模板"
      p "File.dirname="+r
      return r
      # p "File.join(r,f)="+File.join(r,f)
      # return File.join(r,f)
    end
  end
  def 清除未使用
    # p "hello 清除未使用!"
    设置常用对象
    # @model = Sketchup.active_model
    之前数量=@model.definitions.length
    p  "现有definitions数量："+@model.definitions.length.to_s
    @model.definitions.purge_unused
    之后数量=@model.definitions.length
    清除组件数量=之前数量-之后数量
    p  "清除未使用后，剩下definitions数量：" + @model.definitions.length.to_s

    之前数量=@model.materials.length
    @model.materials.purge_unused
    之后数量=@model.materials.length
    清除材料数量=之前数量-之后数量
    Sketchup::set_status_text "清除组件:"+清除组件数量.to_s+"  清除材质："+清除材料数量.to_s


  end

  def 含子组件?(e)
    entities=e.definition.entities
    entities.each {|e|
      if ( e.is_a?( Sketchup::ComponentInstance ) || e.is_a?( Sketchup::Group ))
        return true
      end
    }
    return false
  end
  def 获取子组件(e)
    子组件arr=[]
    if e.kind_of? Sketchup::Model
      entities=e.entities
    else
      entities=e.definition.entities
    end

    entities.each {|e|
      if ( e.is_a?( Sketchup::ComponentInstance ) || e.is_a?( Sketchup::Group ))
        子组件arr.push e
      end
    }
    return 子组件arr
  end

  def 炸开所有子组件主体(e,当前层级)
    @层级h[e]=当前层级
    entities=e.definition.entities
    entities.each {|e|
      if ( e.is_a?( Sketchup::ComponentInstance ) || e.is_a?( Sketchup::Group ))
        当前层级+=1
        炸开所有子组件主体(e,当前层级)
      end
    }
    #递归到底，开始返回
    if @层级h[e]>0
      提示信息="炸开:"+e.definition.name
      p 提示信息
      发送提示信息(提示信息)
      e.explode
    end
  end
  def 发送提示信息(提示信息)
    # p 提示信息=="方法完成"
    if 提示信息=="方法完成"
      上个被调用方法名=caller_locations(1,1)[0].label
      # p 上个被调用方法名
      提示信息=上个被调用方法名+"  完成!"
    else
      提示信息=提示信息
    end
    # puts 提示信息
    Sketchup::set_status_text 提示信息
  end
  def 文件改名(替换字符)
    # chosen_folder = UI.select_directory(title: "选文件夹",directory: "d:")
    chosen_folder = UI.select_directory(title: "请选择 拟改名文件所在 文件夹")
  end

  # def 获取边界框(e)
  #   if e.is_a? Sketchup::Group
  #     边界框 = Geom::BoundingBox.new
  #     e.entities.each {|en| 边界框.add(en.bounds) }
  #   elsif e.is_a? Sketchup::ComponentInstance
  #     边界框 = e.definition.bounds
  #   end
  #   return 边界框
  # end

  def 获取边界大小(e)
    # return unless e.respond_to?(:transformation)
    scale_x = ((Geom::Vector3d.new 1,0,0).transform! e.transformation).length
    scale_y = ((Geom::Vector3d.new 0,1,0).transform! e.transformation).length
    scale_z = ((Geom::Vector3d.new 0,0,1).transform! e.transformation).length
    bb = nil
    if e.is_a? Sketchup::Group
        bb = Geom::BoundingBox.new
        e.entities.each {|en| bb.add(en.bounds) }
    elsif e.is_a? Sketchup::ComponentInstance
        bb = e.definition.bounds
    end

    if bb
        dims = [
            width  = (bb.width  * scale_x),
            height = (bb.height * scale_y),
            depth  = (bb.depth  * scale_z),
            # width  = (bb.width  * scale_x).to_mm.round(2),
            # height = (bb.height * scale_y).to_mm.round(2),
            # depth  = (bb.depth  * scale_z).to_mm.round(2),
            bb,
            [scale_x,scale_y,scale_z]
        ]
        # puts "scale_x:\t#{scale_x}"
        # puts "scale_y:\t#{scale_y}"
        # puts "scale_z:\t#{scale_z}"
        return dims
    end
  end
  def 获取边界大小排除部分组件(e,排除项目)
    # return unless e.respond_to?(:transformation)
    scale_x = ((Geom::Vector3d.new 1,0,0).transform! e.transformation).length
    scale_y = ((Geom::Vector3d.new 0,1,0).transform! e.transformation).length
    scale_z = ((Geom::Vector3d.new 0,0,1).transform! e.transformation).length
    bb = nil
    if e.is_a? Sketchup::Group
        bb = Geom::BoundingBox.new
        e.entities.each {|en|
          # if en.is_a? Sketchup::Group
          #   组件实例名=en.name
          #   # 关键词位置= 组件加实例名=~ /三节抽屉|通用背板|吊柜|地柜/
          #   关键词位置= 组件实例名=~ 排除项目
          #   # p 关键词位置
          #   if 关键词位置!=nil
          #     # puts "属于排除项目：#{组件实例名}"
          #     next
          #   end
          # end
          # if en.is_a? Sketchup::Group
          #   next
          # end
          if en.is_a? Sketchup::Face
            bb.add(en.bounds)
          end
        }
    end
    if bb
        dims = [
            width  = bb.width  * scale_x,
            height = bb.height * scale_y,
            depth  = bb.depth  * scale_z,
            bb
        ]
        # puts "x尺寸:\t#{dims[0].to_l}\ny尺寸:\t#{dims[1].to_l}\nz尺寸:\t#{dims[2].to_l}"
        return dims
    end
  end
end
